package com.yaorange.jk.shiro;

import com.yaorange.jk.utils.Encrypt;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * @author coach tam
 * @date 2017/11/3
 */
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {
    /**
     * 做密码比较
     * @param token 表单用户信息
     * @param info  数据用户信息
     * @return true:一致/false:不一致
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        System.out.println("开始做密码比较");
        boolean result = false;
        UsernamePasswordToken uptoken = (UsernamePasswordToken)token;
        String username = uptoken.getUsername();
        //1,拿到表单的明文密码
        String password = new String(uptoken.getPassword());

        //2,拿到数据库的加密密码
        String dbPassword = (String)info.getCredentials();

        //3,将表单明文密码加密
        password = Encrypt.md5(password,username);


        //4,将两个加密密码进行比较
        result = dbPassword.equals(password);

        return result;
    }
}
